package com.yuzhenxin.bookstore.web.servlet;

import cn.hutool.core.lang.UUID;
import com.example.framework.common.exception.ServiceException;
import com.yuzhenxin.bookstore.domain.*;
import com.yuzhenxin.bookstore.service.OrderService;
import com.yuzhenxin.bookstore.tools.servlet.BaseServlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/OrderServlet")
public class OrderServlet extends BaseServlet {

    private final OrderService orderService = new OrderService();

    /**
     * 加载订单
     */
    public String load(HttpServletRequest request)
            throws ServletException, IOException {
        /*
         * 1、得到oid
         * 2、使用oid调用service方法得到order
         * 3、保存到request域，转发
         */
        request.setAttribute("order", orderService.load(request.getParameter("oid")));
        return "f:/jsps/order/desc.jsp";
    }

    /**
     * 我的订单
     */
    public String myOrders(HttpServletRequest request) {
        /*
         * 1、从session得到当前用户，获取其uid
         * 2、使用uid调用orderService#myOrders得到该用户所有订单
         * 3、把订单保存到request域中，转发
         */
        User user = (User) request.getSession().getAttribute("session_user");
        List<Order> orderList = orderService.myOrders(user.getUid());
        request.setAttribute("orderList", orderList);
        return "f:/jsps/order/list.jsp";
    }

    public String add(HttpServletRequest request)
            throws ServletException, IOException {
        /*
          1、从session得到cart
          2、使用cart生成Order对象
          3、调用service方法完成添加订单
          4、保存Order到request域中，转发
         */
        /*
         * cart --> order
         */
        Cart cart = (Cart) request.getSession().getAttribute("cart");

        Order order = new Order();
        order.setOid(UUID.fastUUID().toString());
        order.setOrdertime(new Date());
        order.setState(1);
        User user = (User) request.getSession().getAttribute("session_user");
        order.setOwner(user);
        order.setTotal(cart.getTotal());

        /*
          创建订单条目集合
          cartItemList --> orderItemList
         */
        List<OrderItem> orderItemList = new ArrayList<>();
        //遍历每一个CartItem，创建各自的OrderItem对象，并添加到集合中
        for (CartItem cartItem : cart.getCartItems()) {
            OrderItem oi = new OrderItem();

            oi.setIid(UUID.fastUUID().toString());
            oi.setCount(cartItem.getCount());
            oi.setBook(cartItem.getBook());
            oi.setSubtotal(cartItem.getSubtotal());
            oi.setOrder(order);

            orderItemList.add(oi);
        }

        // 把所有订单条目添加到订单中
        order.setOrderItemList(orderItemList);

        // 清空购物车
        cart.clear();

        /*
         * 调用orderService添加订单
         */
        orderService.add(order);

        request.setAttribute("order", order);

        return "/jsps/order/desc.jsp";
    }

    public String confirm(HttpServletRequest request) {
        String oid = request.getParameter("oid");
        try {
            orderService.confirm(oid);
            request.setAttribute("msg", "确认成功！");
        } catch (ServiceException e) {
            request.setAttribute("msg", e.getMessage());
        }
        return "f:/jsps/msg.jsp";
    }
}
